package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.mapper.OrganizationMapper;
import com.xmy.cultivate.service.IChangeSchoolDetailService;
import com.xmy.cultivate.service.IRecordCourseService;
import com.xmy.cultivate.service.IStaffService;
import com.xmy.cultivate.service.IStudentService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 转校详情 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2024-04-06
 */
@RestController
@RequestMapping("/api/changeSchoolDetail")
public class ChangeSchoolDetailController {

    @Autowired
    IChangeSchoolDetailService iChangeSchoolDetailService;

    @Autowired
    IRecordCourseService iRecordCourseService;

    @Autowired
    @Lazy
    OrganizationMapper organizationMapper;

    @Autowired
    @Lazy
    IStudentService iStudentService;

    @Autowired
    @Lazy
    IStaffService iStaffService;


    @GetMapping("changeSchoolDetailList")
    @ApiOperation("转校记录")
    public Result changeSchoolDetailList(@RequestParam Map<String, Object> reMap) {
        int pageNum = reMap.get("pageNum") != null && !reMap.get("pageNum").toString().isEmpty() ? Integer.parseInt(reMap.get("pageNum").toString()) : 1;
        int pageSize = reMap.get("pageSize") != null && !reMap.get("pageSize").toString().isEmpty() ? Integer.parseInt(reMap.get("pageSize").toString()) : 10;
        IPage<ChangeSchoolDetail> iPage = new Page<>(pageNum, pageSize);
        String schoolId = reMap.get("schoolId") != null && !reMap.get("schoolId").toString().isEmpty() ? reMap.get("schoolId").toString() : "" ;
        String yearPart = reMap.get("yearPart") != null && !reMap.get("yearPart").toString().isEmpty() ? reMap.get("yearPart").toString() : "" ;
        String quarterNum = reMap.get("quarterNum") != null && !reMap.get("quarterNum").toString().isEmpty() ? reMap.get("quarterNum").toString() : "" ;
        String yearClassId = reMap.get("yearClassId") != null && !reMap.get("yearClassId").toString().isEmpty() ? reMap.get("yearClassId").toString() : "" ;
        String type = reMap.get("type") != null && !reMap.get("type").toString().isEmpty() ? reMap.get("type").toString() : "" ;
        String studentName = reMap.get("studentName") != null && !reMap.get("studentName").toString().isEmpty() ? reMap.get("studentName").toString() : "" ;
        QueryWrapper<ChangeSchoolDetail> changeSchoolDetailQueryWrapper = new QueryWrapper<>();
        changeSchoolDetailQueryWrapper.inSql(StringUtils.isNotBlank(studentName), "student_id", "SELECT id FROM student WHERE deleted = 0 AND name LIKE '%" + studentName + "%'");
        changeSchoolDetailQueryWrapper.eq("deleted",0);
        changeSchoolDetailQueryWrapper.eq(StringUtils.isNotBlank(yearPart), "year_part", yearPart);
        changeSchoolDetailQueryWrapper.eq(StringUtils.isNotBlank(quarterNum), "quarter_num", quarterNum);
        if (StringUtils.isNotBlank(schoolId)) {
            changeSchoolDetailQueryWrapper.and(q -> {
                q.eq("out_school_id", schoolId).or().eq("in_school_id", schoolId);
            });
        }
        changeSchoolDetailQueryWrapper.eq(StringUtils.isNotBlank(yearClassId), "year_class_id", yearClassId);
        changeSchoolDetailQueryWrapper.eq(StringUtils.isNotBlank(type), "type", type);
        changeSchoolDetailQueryWrapper.lt("status",2);
        iChangeSchoolDetailService.page(iPage, changeSchoolDetailQueryWrapper);
        for (ChangeSchoolDetail changeSchoolDetail: iPage.getRecords()) {
            //已用课次列表详细
            QueryWrapper<RecordCourse> recordCourseQueryWrapper = new QueryWrapper<>();
            recordCourseQueryWrapper.eq("deleted",0);
//            recordCourseQueryWrapper.and(q -> {
//                q.eq("school_id",changeSchoolDetail.getInSchoolId()).or().eq("school_id",changeSchoolDetail.getOutSchoolId());
//            });
            recordCourseQueryWrapper.eq("school_id",changeSchoolDetail.getOutSchoolId());//这里要转出前就行，转入的不要了
            recordCourseQueryWrapper.eq("year_class_id",changeSchoolDetail.getYearClassId());
            recordCourseQueryWrapper.eq("student_id",changeSchoolDetail.getStudentId());
            recordCourseQueryWrapper.eq("year_part",changeSchoolDetail.getYearPart());
            recordCourseQueryWrapper.eq("quarter",changeSchoolDetail.getQuarterNum());
            recordCourseQueryWrapper.eq("subjects_id",changeSchoolDetail.getSubjectsId());
            List<RecordCourse> recordCourseList = iRecordCourseService.list(recordCourseQueryWrapper);
            changeSchoolDetail.setRecordCourseList(recordCourseList);
//            long useCourseCount = 0L;
//            if (recordCourseList != null && !recordCourseList.isEmpty()) {
//                useCourseCount = recordCourseList.size();
//            }
//            changeSchoolDetail.setUseCourseCount((int) useCourseCount);//总数是后期处理记录的，这里不用重新赋值
            //输出校区名称
            Organization organization = organizationMapper.selectById(changeSchoolDetail.getOutSchoolId());
            if (organization != null) {
                changeSchoolDetail.setOutSchoolName(organization.getName());
            }
            //输入校区名称
            Organization organizationIn = organizationMapper.selectById(changeSchoolDetail.getInSchoolId());
            if (organizationIn != null) {
                changeSchoolDetail.setInSchoolName(organizationIn.getName());
            }
            //学生名称
            Student studentServiceById = iStudentService.getById(changeSchoolDetail.getStudentId());
            if (studentServiceById != null) {
                changeSchoolDetail.setStudentName(studentServiceById.getName());
                changeSchoolDetail.setHeadImg(studentServiceById.getHeadImg());
            }
            //操作人员名称
            Staff staffServiceById = iStaffService.getOneById(changeSchoolDetail.getCreatedId());
            if (staffServiceById != null) {
                changeSchoolDetail.setCreatedName(staffServiceById.getName());
            }
        }
        return new Result(Code.OK,new PageResult<>(iPage.getRecords(), iPage.getTotal(), iPage.getPages()),"列出转校记录成功");
    }


}
